// ⼭峰数组的峰顶
// https://leetcode.cn/problems/peak-index-in-a-mountain-array/
class Solution {
public:
    int peakIndexInMountainArray(vector<int>& arr) 
    {
        int left = 1;
        int right = arr.size() - 2;
        while(left < right)
        {
            int mid = left + (right - left + 1) / 2;
            if(arr[mid] > arr[mid - 1]) left = mid;
            else right = mid - 1;
        }
        return left;
    }
};

// https://leetcode.cn/problems/find-peak-element/
// 寻找峰值
class Solution {
public:
    int findPeakElement(vector<int>& nums) 
    {
        int left = 0;
        int right = nums.size() - 1;
        while(left < right)
        {
            int mid = left + (right - left) / 2;
            if(nums[mid] > nums[mid + 1]) right = mid;
            else left = mid + 1;
        }
        return left;
    }
};

// https://leetcode.cn/problems/find-minimum-in-rotated-sorted-array/
// 搜索旋转排序数组中的最⼩值

class Solution {
public:
    int findMin(vector<int>& nums) 
    {
        int left = 0;
        int right = nums.size() - 1;
        int x = nums[right];
        while(left < right)
        {
            int mid = left + (right - left) / 2;
            if(nums[mid] > x) left = mid + 1;
            else right = mid;
        }
        return nums[left];
    }
};


// 0〜n-1 中缺失的数字
// https://leetcode.cn/problems/que-shi-de-shu-zi-lcof/

class Solution {
public:
    int takeAttendance(vector<int>& nums) {
        int left = 0;
        int right = nums.size() - 1;
        while(left < right)
        {
            int mid = left + (right - left) / 2;
            if(nums[mid] == mid) left = mid +1;
            else
                right = mid;
        }
        return nums[left] == left ? left + 1 : left;
    }
};

